package co.yixiang.yshop.module.crm.dal.mysql.crmcloudcalllog;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.mybatis.core.mapper.BaseMapperX;
import co.yixiang.yshop.framework.mybatis.core.query.LambdaQueryWrapperX;
import co.yixiang.yshop.module.crm.controller.admin.crmcloudcall.vo.CrmCloudcallRespVO;
import co.yixiang.yshop.module.crm.controller.admin.crmcloudcalllog.vo.CrmCloudcallLogPageReqVO;
import co.yixiang.yshop.module.crm.controller.admin.crmcloudcalllog.vo.CrmCloudcallLogRespVO;
import co.yixiang.yshop.module.crm.dal.dataobject.crmcloudcall.CrmCloudcallDO;
import co.yixiang.yshop.module.crm.dal.dataobject.crmcloudcalllog.CrmCloudcallLogDO;
import co.yixiang.yshop.module.system.dal.dataobject.user.AdminUserDO;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;

/**
 * 呼叫记录 Mapper
 *
 * @author yshop
 */
@Mapper
public interface CrmCloudcallLogMapper extends BaseMapperX<CrmCloudcallLogDO> {

    default PageResult<CrmCloudcallLogRespVO> selectPage(CrmCloudcallLogPageReqVO reqVO) {
        MPJLambdaWrapper<CrmCloudcallLogDO> mpjLambdaWrapper =  new MPJLambdaWrapper<>();
        mpjLambdaWrapper.selectAll(CrmCloudcallLogDO.class)
                .selectAs(AdminUserDO::getNickname,CrmCloudcallLogRespVO::getAdminName)
                .leftJoin(AdminUserDO.class, AdminUserDO::getId, CrmCloudcallLogDO::getAdminId)
                .likeIfExists(AdminUserDO::getNickname, reqVO.getAdminName())
                .likeIfExists(CrmCloudcallLogDO::getName, reqVO.getName())
                .orderByDesc(CrmCloudcallDO::getId);
        if(reqVO.getCreateTime() != null){
            mpjLambdaWrapper.between(CrmCloudcallLogDO::getCreateTime,
                    reqVO.getCreateTime()[0],
                    reqVO.getCreateTime()[1]);
        }
        return selectJoinPage(reqVO, CrmCloudcallLogRespVO.class,mpjLambdaWrapper);
    }

    default PageResult<CrmCloudcallLogDO> selectPage2(CrmCloudcallLogPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<CrmCloudcallLogDO>()
                .likeIfPresent(CrmCloudcallLogDO::getName, reqVO.getName())
                .eqIfPresent(CrmCloudcallLogDO::getSource, reqVO.getSource())
                .eqIfPresent(CrmCloudcallLogDO::getSourceId, reqVO.getSourceId())
                .eqIfPresent(CrmCloudcallLogDO::getFromExten, reqVO.getFromExten())
                .eqIfPresent(CrmCloudcallLogDO::getStatus, reqVO.getStatus())
                .eqIfPresent(CrmCloudcallLogDO::getCallNo, reqVO.getCallNo())
                .eqIfPresent(CrmCloudcallLogDO::getCalledNo, reqVO.getCalledNo())
                .eqIfPresent(CrmCloudcallLogDO::getRecordFile, reqVO.getRecordFile())
                .eqIfPresent(CrmCloudcallLogDO::getCallTimeLength, reqVO.getCallTimeLength())
                .eqIfPresent(CrmCloudcallLogDO::getActionId, reqVO.getActionId())
                .betweenIfPresent(CrmCloudcallLogDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(CrmCloudcallLogDO::getId));
    }

}